///*
// * author      : 吴中勤
// * date        : 2019/3/11
// * description : TODO
// */
//
//package org.wzq.learn;
//
//import brave.Tracer;
//import org.springframework.cloud.sleuth.instrument.web.TraceWebServletAutoConfiguration;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//import org.springframework.web.filter.GenericFilterBean;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.ServletRequest;
//import javax.servlet.ServletResponse;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletRequestWrapper;
//import java.io.IOException;
//import java.util.*;
//
///**
// * 设置 请求头  ((HttpServletResponse) response).addHeader("X-B3-Flags", "1")
// * 这样 当前 span 能够必然采样
// *
// * 但是解决不了我的问题: 异常时采样
// * - 1 异常时不在filter中处理
// * - 2 时序不对, 设置请求头
// */
//@Component
//@Order(TraceWebServletAutoConfiguration.TRACING_FILTER_ORDER - 1)
//public class MyZipkinFilterTest extends GenericFilterBean {
//
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response,
//                         FilterChain chain) throws IOException, ServletException {
//
//        System.out.println("MyZipkinFilterTest filter");
//
//        HttpServletRequest req = (HttpServletRequest) request;
//        HeaderMapRequestWrapper requestWrapper = new HeaderMapRequestWrapper(req);
//
//
//        // 如果 http code >= 400, 强制采样 http://cloud.spring.io/spring-cloud-sleuth/2.0.x/multi/multi__sampling.html
//        // You can set the HTTP header X-B3-Flags to 1
//        // 实际发现不行, 及时无条件添加 response header X-B3-Flags to 1
////        if (((HttpServletResponse) response).getStatus() >= HttpStatus.BAD_REQUEST.value()) {
////            System.out.printf("强制采样");
////            ((HttpServletResponse) response).addHeader("X-B3-Flags", "1");
////        }
//        requestWrapper.addHeader("X-B3-Flags", "1");
//        chain.doFilter(requestWrapper, response);
//
//        // 有价值的尝试
//        // ===以下没用===
////        try{
////            chain.doFilter(requestWrapper, response); // Goes to default servlet.
////        }catch (Exception ex) {
////            requestWrapper.addHeader("X-B3-Flags", "1");
////            throw ex;
////        }
//
//    }
//
//
//    public static class HeaderMapRequestWrapper extends HttpServletRequestWrapper {
//        public HeaderMapRequestWrapper(HttpServletRequest request) {
//            super(request);
//        }
//
//        private Map<String, String> headerMap = new HashMap<String, String>();
//
//        public void addHeader(String name, String value) {
//            headerMap.put(name, value);
//        }
//
//        @Override
//        public String getHeader(String name) {
//            String headerValue = super.getHeader(name);
//            if (headerMap.containsKey(name)) {
//                headerValue = headerMap.get(name);
//            }
//            return headerValue;
//        }
//
//        /**
//         * get the Header names
//         */
//        @Override
//        public Enumeration<String> getHeaderNames() {
//            List<String> names = Collections.list(super.getHeaderNames());
//            for (String name : headerMap.keySet()) {
//                names.add(name);
//            }
//            return Collections.enumeration(names);
//        }
//
//        @Override
//        public Enumeration<String> getHeaders(String name) {
//            List<String> values = Collections.list(super.getHeaders(name));
//            if (headerMap.containsKey(name)) {
//                values.add(headerMap.get(name));
//            }
//            return Collections.enumeration(values);
//        }
//
//    }
//
//}